package com.baozun.houji.me.activity.account

import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.util.eventbus.EventBusUtils
import com.baozhun.mall.common.util.eventbus.EventCode
import com.baozhun.mall.common.util.eventbus.EventMessage
import com.baozhun.mall.common.util.onkeylogin.OneKeyLoginHelper
import com.baozun.houji.me.R
import com.baozun.houji.me.databinding.ActivityLoginBinding
import com.baozun.houji.me.fragment.PhoneLoginFragment
import com.baozun.houji.me.viewmodel.LoginViewModel
import com.jaeger.library.StatusBarUtil
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * @Title：登录页面
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2022/12/10
 * @Version：1.0.0
 */
@Route(path = RoutePath.Me.LOGIN)
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {

    private val phoneLonginFragment by lazy { PhoneLoginFragment.newInstance() }

    override fun initView(savedInstanceState: Bundle?) {
        EventBusUtils.register(this)
        StatusBarUtil.setColor(this, getColor(android.R.color.transparent))
        StatusBarUtil.setTransparentForImageView(this, mDataBinding.titleBarBack)
        mDataBinding.cbLoginProtocol.movementMethod = LinkMovementMethod.getInstance()
        mDataBinding.cbLoginProtocol.highlightColor =
            getColor(android.R.color.transparent) //方法重新设置文字背景为透明色。
        mDataBinding.cbLoginProtocol.text = OneKeyLoginHelper.getInstance().getProtocolSpannableString("我已阅读并同意履行")
        changeFragment(phoneLonginFragment)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.title_bar_back -> {
                finish()
            }
        }
    }

    /**
     * 切换登陆fragment
     */
    private fun changeFragment(fragment: Fragment) {
        var fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(mDataBinding.fragmentContainer.id, fragment)
        fragmentTransaction.commitNowAllowingStateLoss()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage<Any>) {
        if (event.eventCode == EventCode.LOGIN_SUCCESS) {
            finish()
        }
    }
}